package com.zds.hj.poseidon.biz.dispatcher.credit.wycHandler;

import com.zds.boot.dispatch.annotation.DispatcherHandler;
import com.zds.boot.dispatch.comm.enums.InstructTypeEnum;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.*;
import com.zds.hj.icmc.facade.dto.capitallnfo.CapitalInfoDto;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.layout.CreditApplyService;
import com.zds.hj.poseidon.biz.common.IcmcServiceInterface;
import com.zds.hj.poseidon.biz.dispatcher.credit.CreditDemoHandler;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.hj.product.facade.constant.ApplicationContainerConstant;
import com.zds.zly.common.utils.EmptyChecker;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wyc
 * @site www.yiyang.com
 * @company 王云川有限公司
 * @create 2023-06-07 13:51
 */
@DispatcherHandler(value = "CapitalSideInitHandler", desc = "获取资金方模块", applicationContainer = ApplicationContainerConstant.FRONT_END, nodeType = ModuleNodeTypeEnum.BUSINESS)
@Slf4j
public class CapitalSideInitHandler extends WycMainHandler {
    private static final String CHOICE_RULE = CreditDemoHandler.class.getName() + ".CHOICE_RULE";

    private static final String CHOICE_ROUTE = CreditDemoHandler.class.getName() + ".CHOICE_ROUTE";
    @Resource
    private IcmcServiceInterface icmcServiceInterface;
    @Resource
    private CreditApplyService creditApplyService;

    @Override
    public void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, CreditInfoEntityDto reqData, CreditInfoEntityDto dbData) {

        //通过产品编号获取资金方列表
        List<CapitalInfoDto> capitalByProNo = icmcServiceInterface.getCapitalByProNo(reqData.getProductNo());
        if(EmptyChecker.isEmpty(capitalByProNo)){
            log.info("查询资金方数据为空");
        }
        CapitalInfoDto choiceResult = null;
        Map<String, InstructDto> instructSettingMap = params.getDispatchModuleNodes().getInstructSettingMap();

        //资金方是否强制获取
        InstructDto route = instructSettingMap.get(CHOICE_ROUTE);
        log.info("是否强制获取资金方结果：{}", "1".equals(route.getValues()) ? "是" : "否");

        //资金方获取规则
        InstructDto rule = instructSettingMap.get(CHOICE_RULE);
        log.info("获取资金方规则为：{}", "1".equals(rule.getValues()) ? "是" : "否");
        //逻辑处理
        if ("2".equals(route.getValues())) {
            log.info("根据规则无需强制获取资金方，产品编号：{}", reqData.getProductNo());
            return;
        }
        if ("1".equals(route.getValues())) {
            if (EmptyChecker.isEmpty(dbData) || EmptyChecker.isEmpty(capitalByProNo.get(0).getCapitalSideNo())) {
                log.info("通过{}查询资金方列表失败", reqData.getProductNo());
            }
            else {
                if ("1".equals(rule.getValues())) {
                     choiceResult = capitalByProNo.get(0);
                }
                else if ("2".equals(rule.getValues())) {
                    int randomNum = (int) (Math.random() * capitalByProNo.size());
                    log.info("产生的随机数为{}", randomNum);
                    choiceResult = capitalByProNo.get(randomNum);
                }
                //存储资金方信息
                dbData.setCapitalSideNo(choiceResult.getCapitalSideNo());
                dbData.setCapitalName(choiceResult.getCapitalName());
            }
        }
    }

    @Override
    public ModuleBaseInfo moduleBaseInfo() {
        ModuleBaseInfo moduleBaseInfo = super.moduleBaseInfo();
        moduleBaseInfo.setAuto(true);
        return moduleBaseInfo;
    }


    /**
     * 模块指令
     *
     * @return
     */
    @Override
    public List<InstructRegisterModule> moduleInstructs() {
        List<InstructRegisterModule> instructRegisterModules = new ArrayList<>();

        InstructRegisterModule registerModule0 = new InstructRegisterModule();
        registerModule0.setInstructCode(CHOICE_ROUTE);
        registerModule0.setInstructName("是否强制获取资金方");
        registerModule0.setInstructTitle("是否强制获取资金方");
        registerModule0.setInstructType(InstructTypeEnum.PULL_DOWN_ONE);
        registerModule0.setInstructEmphatic("是否强制获取资金方");
        registerModule0.setValueSource(1);  // 值的来源 自定义值
        registerModule0.setInstructValueByCustom("1@是^2@否");
        registerModule0.setEmptyCheck(true);
        instructRegisterModules.add(registerModule0);


        InstructRegisterModule registerModule1 = new InstructRegisterModule();
        registerModule1.setInstructCode(CHOICE_RULE);
        registerModule1.setInstructName("获取资金方");
        registerModule1.setInstructTitle("获取资金方");
        registerModule1.setInstructType(InstructTypeEnum.PULL_DOWN_ONE);
        registerModule1.setInstructEmphatic("获取资金方");
        registerModule1.setValueSource(1);  // 值的来源 自定义值
        registerModule1.setInstructValueByCustom("1@固定^2@轮询");
        registerModule1.setEmptyCheck(true);
        instructRegisterModules.add(registerModule1);
        return instructRegisterModules;
    }

}
